﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jacaranda.Model
{
    class SelectMatchCriteria : ConditionalExpression
    {
        public SelectMatchCriteria(string entity, string field, ConditionalExpression condition)
        {
            Entity = entity;
            Field = field;
            Condition = condition;
        }


        public string Entity { get; private set; }

        public string Field { get; private set; }

        public ConditionalExpression Condition { get; private set; }


        internal override string GenerateSql(Type propertyEntityType)
        {
            EntityMetadata target = Metadata.RegisteredTypes.First(i => i.Type.Name == Entity);
            return string.Format("SELECT {0} FROM {1} {2} {3}",
                target.Fields.First(i => i.PropertyName == Field).FieldName,
                target.TableName,
                (Condition == null) ? null : "WHERE",
                Condition.GenerateSql(propertyEntityType));
        }
    }
}
